package com.han.transfer.util;

import com.han.transfer.constant.BaseParameter;
import lombok.Setter;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * @author HanJiafeng
 * @date 2021/6/9 11:07
 * @desc 画家
 */
public class Painter {
    /**
     * 默认字体
     */
    @Setter
    private static BaseParameter.FontName defaultFrontName = BaseParameter.FontName.HAN_YI_FANG_SONG_JIAN;

    /**
     * 将给定content转换成指定width图片,height自适应
     *
     * @param height          高度
     * @param content         内容
     * @param fontColor       字体颜色
     * @param backgroundColor 背景颜色
     * @return 图片
     */
    public static Image draw(int height, String content, int style, Color fontColor, Color backgroundColor) {
        int imageWidth = 10000;
        int imageHeight = 500;
        BufferedImage payeeImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D payeeGraphics2D = payeeImage.createGraphics();
        //设置背景颜色
        payeeGraphics2D.setColor(backgroundColor);
        payeeGraphics2D.fillRect(0, 0, imageWidth, imageHeight);
        //设置字体颜色
        payeeGraphics2D.setColor(fontColor);
        payeeGraphics2D.setFont(new Font(defaultFrontName.toString(), style, 500));
        payeeGraphics2D.drawString(content, 0, 429);
        //裁剪图片
        payeeImage = cutWhiteImage(payeeImage, backgroundColor);
//        Tool.save(payeeImage,"E:\\截图\\" + System.currentTimeMillis() + ".png");
        //根据裁剪后的图片比例，计算高度
        int scaleWidth = (int) (((double) payeeImage.getWidth() / (double) payeeImage.getHeight()) * (double) height);
        //缩放图片
        return payeeImage.getScaledInstance(scaleWidth, height, Image.SCALE_SMOOTH);
    }

    /**
     * 将给定content转换成指定width图片,height自适应，字体颜色默认黑色，背景颜色默认白色
     *
     * @param height  高度
     * @param content 内容
     * @return 图片
     */
    public static Image draw(int height, String content) {
        return draw(height, content, Font.PLAIN, Color.black, Color.white);
    }

    /**
     * 将给定content转换成指定width图片,height自适应，字体颜色默认黑色，背景颜色默认白色
     *
     * @param height  宽度
     * @param content 内容
     * @return 图片
     */
    public static Image draw(int height, String content, Color foreColor) {
        return draw(height, content, Font.PLAIN, foreColor, Color.white);
    }

    /**
     * 将给定content转换成指定width图片,height自适应
     *
     * @param height    宽度
     * @param content   内容
     * @param foreColor 字体颜色
     * @param backColor 背景颜色
     * @return 图片
     */
    public static Image draw(int height, String content, Color foreColor, Color backColor) {
        return draw(height, content, Font.PLAIN, foreColor, backColor);
    }

    /**
     * 切掉图片右侧和下侧的空白部分
     *
     * @param bufferedImage 金额图片
     * @return BufferedImage
     */
    public static BufferedImage cutWhiteImage(BufferedImage bufferedImage, Color bgColor) {

        int[][] imageRgbArray = new int[bufferedImage.getWidth()][bufferedImage.getHeight()];
        int maxX = 0;
        int maxY = 0;
        //通过getRGB()方式获得像素矩阵，通过纵向检查有像素的最大x坐标和y坐标
        for (int i = 0; i < bufferedImage.getWidth(); i++) {
            for (int j = 0; j < bufferedImage.getHeight(); j++) {
                int rgb = imageRgbArray[i][j] = bufferedImage.getRGB(i, j);
                if (rgb != bgColor.getRGB() && maxX < i) {
                    maxX = i;
                }
                if (rgb != bgColor.getRGB() && maxY < j) {
                    maxY = j;
                }
            }
        }

        //将二维数组转换为一维数组
        int[] tempRgbArray = new int[maxX * maxY];
        int k = 0;
        for (int j = 0; j < maxY; j++) {
            for (int i = 0; i < maxX; i++) {
                tempRgbArray[k++] = imageRgbArray[i][j];
            }
        }

        //以maxX和maxY新建一个BufferedImage
        bufferedImage = new BufferedImage(maxX, maxY, BufferedImage.TYPE_INT_BGR);
        bufferedImage.setRGB(0, 0, maxX, maxY, tempRgbArray, 0, maxX);

        return bufferedImage;
    }
}
